package com.lagou.dao.impl;

import com.lagou.dao.CourseLessonDao;
import com.lagou.poji.Course_Lesson;
import com.lagou.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;

import java.sql.SQLException;

public class CourseLessonImpl implements CourseLessonDao {
    @Override
    public int saveLesson(Course_Lesson lesson) {
        try {
            QueryRunner qr=new QueryRunner(DruidUtils.getDataSource());

            String sql =
                    "INSERT INTO course_lesson(course_id,section_id,theme,duration,is_free,order_num,create_time,update_time,is_del)\n" +
                    "VALUE(?,?,?,?,?,?,?,?,?)";
            Object[] param = {lesson.getCourse_id(),lesson.getSection_id(),lesson.getTheme(),lesson.getDuration(),lesson.getIs_free(),lesson.getOrderNum(),lesson.getCreate_time(),lesson.getUpdate_time(),lesson.getIsDel()
            };

            int i = qr.update(sql,param);
            return i;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int updateLesson(Course_Lesson lesson) {
        try {
            QueryRunner qr=new QueryRunner(DruidUtils.getDataSource());
            String sql = "UPDATE course_lesson SET course_id=?,section_id=?,theme=?,duration=?,is_free=?,order_num=?,update_time=? WHERE id=?";
            Object[] param={lesson.getCourse_id(),lesson.getSection_id(),lesson.getTheme(),lesson.getDuration(),lesson.getIs_free(),lesson.getOrderNum(),lesson.getUpdate_time(),lesson.getId()};
            int i = qr.update(sql,param);
            return i;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }
}
